package hotLeetcode;

import java.util.ArrayList;
import java.util.List;

public class H17Leetcode {

    List<String> res = new ArrayList<>();
    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return res;
        }
        //初始对应所有的数字，为了直接对应2-9，新增了两个无效的字符串""
        String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

        backTracking(digits, numString, 0);

        return res;

    }

    StringBuilder temp = new StringBuilder();

    private void backTracking(String digits, String[] numString, int num) {
         if (num == digits.length()) {
            res.add(temp.toString());
            return;
        }

        //找到当前的字符串
        String str = numString[digits.charAt(num) - '0'];
        for (int i = 0; i < str.length(); i++) {
            temp.append(str.charAt(i));
            backTracking(digits, numString, num + 1);
            temp.deleteCharAt(temp.length() - 1);
        }


    }

    public static void main(String[] args) {
        String digits = "23";
        H17Leetcode h17Leetcode = new H17Leetcode();
        System.out.println(h17Leetcode.letterCombinations(digits));

    }
}
